package org.mypro.front;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mypro.dao.KhMapper;
import org.mypro.dao.UserMapper;
import org.mypro.entity.Kh;
import org.mypro.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/")
public class IndexController {

	private static final Log logger = LogFactory.getLog(IndexController.class);

	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private KhMapper khDao;
	
	@RequestMapping(value = "index")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("IndexController.Index ......");
		return "houtai/index";
	}

	@RequestMapping(value = "regedit", method = RequestMethod.GET)
	public String regedit() {
		return "regedit";
	}

	@RequestMapping(value = "regedit", method = RequestMethod.POST)
	public ModelAndView regedit(Kh user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("regedit");
		try {
			if (khDao.countList(user) <= 0) {
				user.setCreatetime(new Date());
				khDao.insert(user);
				mv.addObject("message", "注册成功,请登录！");
				mv.addObject("status", true);
			} else {
				mv.addObject("message", "客户已经存在，请更换用户名再试！");
				mv.addObject("status", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "系统错误，请联系系统管理员！");
			mv.addObject("status", false);
		}
		return mv;

	}

	/**
	 * 登陆引导
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "login")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		return "login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request, User user) {
		if (StringUtils.isNotEmpty(user.getUsername())) {
			List<User> users = userDao.findLogin(user);
			if (!users.isEmpty()
					&& users.get(0).getPassword().equals(user.getPassword())
					&& StringUtils.isNotEmpty(users.get(0).getIsenable())
					&& users.get(0).getIsenable().equals("启用")) {
				request.getSession(true).setAttribute("LOGIN_USER",
						users.get(0));
				return "redirect:/index.action";
			} else if (!users.isEmpty()
					&& StringUtils.isNotEmpty(users.get(0).getIsenable())
					&& users.get(0).getIsenable().equals("禁用")) {
				request.setAttribute("message", "该用户被禁用！");
				return "login";
			}
			request.setAttribute("message", "用户名或者密码错误！");
			return "login";
		} else {
			request.setAttribute("message", "用户名或者密码不能为空！");
			return "login";
		}
	}

	@RequestMapping(value = "loginout")
	public String loginout(HttpServletRequest request) {
		request.getSession().removeAttribute("LOGIN_USER");
		return "redirect:/index.action";
	}
}
